package com.x.example.page;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.x.example.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ShortcutHelper {
    private static final String ID_1 = "id_1";
    private static final String ID_2 = "id_2";
    private static final String ID_3 = "id_3";
    private static final String ID_4 = "id_4";

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    public static ShortcutInfo generateShortcut(Context context, String id) {
        return new ShortcutInfo.Builder(context, id)
                .setShortLabel("动态-" + id)
                .setLongLabel("动态长思考噶临时工-" + id)
                .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
                .setIntent(new Intent() {
                    {
                        setAction(Intent.ACTION_VIEW);
                        setClassName("com.wy.xpage.example", "com.wy.xpage.example.MainActivity");
                        putExtra("jake", "hahaha");
                    }
                })
                .build();
    }

    public static void checkAndCreateShortcuts(Context context) {
        if (context == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
            return;
        }
        ShortcutManager manager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
        List<ShortcutInfo> dynamicShortcuts = new ArrayList<>();
        dynamicShortcuts.add(generateShortcut(context, ID_1));
        dynamicShortcuts.add(generateShortcut(context, ID_2));
        dynamicShortcuts.add(generateShortcut(context, ID_3));
        dynamicShortcuts.add(generateShortcut(context, ID_4));
        manager.setDynamicShortcuts(dynamicShortcuts);
        manager.disableShortcuts(Arrays.asList(ID_1,ID_2));
    }

}
